/**
 *  * Created by wz on 2017/9/26
 *  * <p/>
 *  * Copyright (c) 2015-2015
 *  * Apache License
 *  * Version 2.0, January 2004
 *  * http://www.apache.org/licenses/
 *  
 */
package com.huirui.service;

import com.huirui.dao.UserDAO;
import com.huirui.entity.Identity;
import com.huirui.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserService {

    @Autowired
    UserDAO userDAO;

//    public User login(User user) {
//        User u = userDAO.findByUsernameAndPassword(user.getUsername(), user.getPassword());
//        return u;
//    }

    public User loginWithIdentity(User user) {
        return userDAO.findByUsernameAndPasswordAndIdentity(user.getUsername(),
                user.getPassword(), user.getIdentity());
    }


    @Transactional
    public void insertUser(User user) {
        userDAO.save(user);

        if(user.getIdentity().getIdentityId()<10){
            throw new RuntimeException("transaction test");
        }
        user.setUsername(user.getUsername() + "test");
        userDAO.saveAndFlush(user);
    }

    public List<User> queryByIdentity(Identity identity) {
        return userDAO.findByIdentity(identity);
    }


}
